﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using University.Workshop.Model;
using System.Threading;
using System.Media;
using System.IO;
using System.Globalization;
using University.Workshop.Business;
using System.Collections.ObjectModel;
using University.Workshop.DataAccess.LanguagePack.ClientLanguagePack;
using University.Workshop.DataAccess;

namespace University.Workshop.ClientApp.MiniGames.Roulette
{
    /// <summary>
    /// Interaction logic for Roulette.xaml
    /// </summary>
    public partial class Roulette : Window
    {
        //private SoundPlayer Effect;
        private RouletteManager ManagerList;
        private long Amount;
        private long AmountLose;
        private string LanguageSelected;
        private User userPointer;
        private Collection<Button> ListButtons; 
        //private string PathAudioEffect = Directory.GetCurrentDirectory() + "\\Audio\\WheelAndBall_Effect.wav";

        private string MessageRoutingWheel;
        private string MessageItemRaffle;
        private string MessageYouDontHaveChips;
        private string MessageTheMaximunBetIs;	


        private int MaximunBet;
        private int PermitBet = 6;

        public Roulette()
        {
            InitializeComponent();
            Initialize_ComboBox();
            ListButtons = new Collection<Button>();
            MaximunBet = 0;
            //Effect = new SoundPlayer(this.PathAudioEffect);
        }

        public Roulette(string languageSelected)
        {
            InitializeComponent();
            
            this.LanguageSelected = languageSelected;
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(this.LanguageSelected);
            ApplyLanguage();

            Initialize_ComboBox();
            ListButtons = new Collection<Button>();
            MaximunBet = 0;
            //Effect = new SoundPlayer(this.PathAudioEffect);
        }

        public Roulette(User user, long amount, string languageSelected)
        {
            this.Amount = amount;
            this.AmountLose = amount;

            InitializeComponent();

            this.LanguageSelected = languageSelected;
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(this.LanguageSelected);
            ApplyLanguage();

            ManagerList = new RouletteManager();
            ManagerList.NewTable();
            this.textBoxTotal.Text = Amount.ToString();
            ListButtons = new Collection<Button>();
            MaximunBet = 0;
            //Effect = new SoundPlayer(this.PathAudioEffect);

            //Ellipse Hidden
            EllipseBall.Visibility = Visibility.Hidden;

            this.userPointer = user;
            this.groupBoxUserInformation.Header = user.Name;
            Initialize_ComboBox();            

            if (amount <= 50)
            {
                this.Close();
            }            
        }

        private void Initialize_ComboBox()
        {
            //Here the amount to bet
            this.comboBoxBet.Items.Add(50);
            this.comboBoxBet.Items.Add(100);
            this.comboBoxBet.Items.Add(200);
            this.comboBoxBet.Items.Add(500);
            this.comboBoxBet.Items.Add(1000);
            this.comboBoxBet.Items.Add(2000);
            this.comboBoxBet.Items.Add(5000);
            this.comboBoxBet.Items.Add(10000);
            this.comboBoxBet.Items.Add(100000);

            this.Table.IsEnabled = false;
            this.comboBoxBet.SelectedIndex = 0;
        }

        private void Completed_Wheel(object sender, EventArgs e)
        {
            this.Table.IsEnabled = true;

            this.listBoxHistory.Items.Add( ManagerList.GetAngle(wheelAngle.Angle.ToString()).Number );
            //this.Effect.Stop();

            MessageBox.Show(this.MessageItemRaffle + ManagerList.GetAngle(wheelAngle.Angle.ToString()).Number, ":-O");

            Collection<String> NewListBet = new Collection<string>();
            Collection<String> NewListChips = new Collection<string>();
            for(int Pointer=0; Pointer<this.listBoxSelectedBet.Items.Count; Pointer++)
            {
                NewListBet.Add(this.listBoxSelectedBet.Items.GetItemAt(Pointer).ToString());
                NewListChips.Add(this.listBoxChipsBet.Items.GetItemAt(Pointer).ToString());
            }
            long Value = ManagerList.GiveMeTheResult(ManagerList.GetAngle(wheelAngle.Angle.ToString()).Number.ToString(), NewListBet, NewListChips);

            if (Value > 0)
            {
                Amount = AmountLose + Value;
                this.textBoxTotal.Text = Amount.ToString();
                this.userPointer.Chips = Amount;
            }
            else
            {
                Amount = AmountLose;
                this.textBoxTotal.Text = Amount.ToString();
                this.userPointer.Chips = Amount;
            }

            for (int ButtonPointer = 0; ButtonPointer < this.ListButtons.Count; ButtonPointer++)
            {
                this.ListButtons[ButtonPointer].Opacity = 0.1;
                string NameButton = this.ListButtons[ButtonPointer].Name;
                NameButton = NameButton.Replace("Button_", "");

                int Position = this.listBoxSelectedBet.Items.IndexOf(NameButton);                

                this.listBoxSelectedBet.Items.Remove(NameButton);
                this.listBoxChipsBet.Items.RemoveAt(Position);                   
            }

            //Empty the collection list
            for (int ButtonPointer = (this.ListButtons.Count -1); ButtonPointer >= 0; ButtonPointer--)
            {
                this.ListButtons.RemoveAt(ButtonPointer);
            }
            

            //Refresh to the data base the new information
            Client client = new Client();            
            string Parameter = "14 " + userPointer.Name + " " + userPointer.Chips.ToString(CultureInfo.InvariantCulture);
            client.Send(Parameter);
            client.Receive();

            //Set the state
            client.SetGameStatus(userPointer, "Roulette");

            this.buttonSpin.IsEnabled = true;
            MaximunBet = 0;
        }


        private void buttonSpin_Click(object sender, RoutedEventArgs e)
        {
            this.listBoxHistory.Items.Add(this.MessageRoutingWheel);

            this.buttonSpin.IsEnabled = false;            

            Random RandomTime = new Random(DateTime.Now.Millisecond);
            long TimeRepeat = (long)RandomTime.Next(10000000, 100000000);

            Storyboard spinStoryboard = Resources["spinWheel"] as Storyboard;
            if (spinStoryboard != null)
            {
                spinStoryboard.RepeatBehavior = new RepeatBehavior(new TimeSpan(TimeRepeat));
                StartWheel();
                this.Table.IsEnabled = false;
                
                //this.Effect.PlayLooping();
            }

        }



        private void PauseWheel()
        {
            Storyboard spinStoryboard = Resources["spinWheel"] as Storyboard;
            if (spinStoryboard != null)
            {
                if (!spinStoryboard.GetIsPaused(this))
                {
                    spinStoryboard.Pause(this);
                }
            }
        }

        private void ResumeWheel()
        {
            Storyboard spinStoryboard = Resources["spinWheel"] as Storyboard;
            if (spinStoryboard != null)
            {
                if (!spinStoryboard.GetIsPaused(this))
                {
                    spinStoryboard.Resume(this);
                }
            }
        }

        private void StartWheel()
        {
            Storyboard spinStoryboard = Resources["spinWheel"] as Storyboard;
            if (spinStoryboard != null)
            {
                EllipseBall.Visibility = Visibility.Visible;
                spinStoryboard.Begin(this, true);                
            }
        }



        private void buttonTable(object sender, RoutedEventArgs e)
        {
            Button ButtonClicked = (Button)sender;
            string NameButton;

            if (this.comboBoxBet.SelectedIndex >= 0)
            {
                long AmountAuxiliar = Convert.ToInt32(this.comboBoxBet.SelectedItem.ToString());
                if (AmountAuxiliar >= this.AmountLose)
                {
                    MessageBox.Show(this.MessageYouDontHaveChips, ":-(");
                    this.comboBoxBet.SelectedIndex = -1;                    
                }
                else
                {
                    
                    if (ButtonClicked.Opacity.CompareTo(0.1) == 0)
                    {
                        if (this.MaximunBet < this.PermitBet)
                        {
                            MaximunBet++;
                            this.ListButtons.Add(ButtonClicked);
                            ButtonClicked.Opacity = 0.7;

                            NameButton = ButtonClicked.Name;
                            NameButton = NameButton.Replace("Button_", "");

                            this.listBoxSelectedBet.Items.Add(NameButton);
                            this.listBoxChipsBet.Items.Add(this.comboBoxBet.SelectedItem.ToString());

                            this.AmountLose = AmountLose - Convert.ToInt32(this.comboBoxBet.SelectedItem.ToString());
                            this.textBoxTotal.Text = AmountLose.ToString();
                        }
                        else
                        {
                            MessageBox.Show(this.MessageTheMaximunBetIs + this.PermitBet, ":-\\");
                        }
                    }
                    else
                    {
                        MaximunBet--;
                        this.ListButtons.Remove(ButtonClicked);
                        ButtonClicked.Opacity = 0.1;

                        NameButton = ButtonClicked.Name;
                        NameButton = NameButton.Replace("Button_", "");

                        int Position = this.listBoxSelectedBet.Items.IndexOf(NameButton);

                        this.AmountLose = AmountLose + Convert.ToInt32(this.listBoxChipsBet.Items.GetItemAt(Position).ToString());

                        this.listBoxSelectedBet.Items.Remove(NameButton);
                        this.listBoxChipsBet.Items.RemoveAt(Position);

                        this.textBoxTotal.Text = AmountLose.ToString();
                    }
                    
                }
            }
            else
            {
                if (ButtonClicked.Opacity.CompareTo(0.1) != 0)
                {
                    ButtonClicked.Opacity = 0.1;
                    this.ListButtons.Remove(ButtonClicked);

                    NameButton = ButtonClicked.Name;
                    NameButton = NameButton.Replace("Button_", "");

                    int Position = this.listBoxSelectedBet.Items.IndexOf(NameButton);

                    this.AmountLose = AmountLose + Convert.ToInt32(this.listBoxChipsBet.Items.GetItemAt(Position).ToString());

                    this.listBoxSelectedBet.Items.Remove(NameButton);
                    this.listBoxChipsBet.Items.RemoveAt(Position);

                    this.textBoxTotal.Text = AmountLose.ToString();
                }
            }
        }

        private void comboBoxBet_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.comboBoxBet.SelectedIndex >= 0)
            {
                long AmountAuxiliar = Convert.ToInt32(this.comboBoxBet.SelectedItem.ToString());
                if (AmountAuxiliar >= this.AmountLose)
                {
                    MessageBox.Show(this.MessageYouDontHaveChips, ":-(");
                    this.comboBoxBet.SelectedIndex = -1;
                    //this.Table.IsEnabled = false;
                }
                else
                {
                    this.Table.IsEnabled = true;
                }
            }
        }


        private void ApplyLanguage()
        {
            this.MessageItemRaffle = ClientLanguagePack.MiniGamesRouletteMessageItemRaffle;
            this.MessageRoutingWheel = ClientLanguagePack.MiniGamesRouletteMessageRoutingWheel;
            this.MessageYouDontHaveChips = ClientLanguagePack.MiniGamesRouletteMessageYouDontHaveChips;
            this.MessageTheMaximunBetIs = ClientLanguagePack.MiniGamesRouletteMessageTheMaximunBetIs;

            //Button
            this.buttonSpin.Content = ClientLanguagePack.MiniGamesRouletteButtonSpin;

            //Labels
            this.label_AmountToBet.Content = ClientLanguagePack.MiniGamesRouletteLabelAmountToBet;
        }
    }
}
